/* Import Protocol 1
 * JSON Representation of the Sector.
 *
 */

ImportModules[1] = {};

ImportModules[1].ImportSector = function(code)
{
  var sector = JSON.decode(code);

  if (!$chk(Global.CachedSectors[sector.map]))
    throw "Unknown Sector";

  var buildings = sector.buildings;
  var planets = sector.planets;
  var starbases = sector.starbases;
  var backgrounds = sector.backgrounds;
  var name = sector.name;
  var map = sector.map;
  var tiles = Global.CachedSectors[map];

  for (var i = 0; i < buildings.length; i++)
  {
    var building = buildings[i];
    buildings[i].x = tiles[building.id].x;
    buildings[i].y = tiles[building.id].y;
  }

  for (var i = 0; i < planets.length; i++)
  {
    var planet = planets[i];
    planets[i].x = tiles[planet.id].x;
    planets[i].y = tiles[planet.id].y;
  }

  for (var i = 0; i < starbases.length; i++)
  {
    var starbase = starbases[i];
    starbases[i].x = tiles[starbase.id].x;
    starbases[i].y = tiles[starbase.id].y;
    starbases[i].type = tiles[starbase.id].type;
  }

  return new Sector(name, buildings, planets, starbases, backgrounds, tiles, map);
}

ImportModules[1].ImportPricelist = function(code)
{
  var pricelist = JSON.decode(code);
  return new Pricelist(pricelist.name, pricelist.buy, pricelist.sell);
}